package cc.vant.wechat.model.packet;

/**
 * @author Vant
 * @version 2018/11/20 下午 2:19
 */
public class Util {
    public static byte[] int2bytes(int num) {
        byte[] buffer = new byte[4];
        //利用移位和与运算将int转换成byte
        for (int i = 0; i < 4; i++) {
            buffer[i] = (byte) ((num >> 8 * i) & 0xff);
        }

        return buffer;
    }

    public static int bytes2int(byte[] buffer, int offset) {
        int num = 0;
        for (int i = 0; i < 4; i++) {
            num += ((int) (buffer[i + offset] & 0xff) << 8 * i);
        }
        return num;
    }

    public static void combine(byte[] buff1, byte[] buff2) {
        byte[] bytes = new byte[buff1.length + buff2.length];
        System.arraycopy(buff1, 0, bytes, 0, buff1.length);
        System.arraycopy(buff2, 0, bytes, 4, buff2.length);
    }
}